//
//  InfoDetailViewController.m
//  MemberProject
//
//  Created by mac on 15/5/14.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "InfoDetailViewController.h"
#import "UMSocial.h"
#import "WeiboSDK.h"
#import "ShareMenuView.h"
#import "AppDelegate.h"
#import "MoreMenuView.h"
@interface InfoDetailViewController ()<UIActionSheetDelegate,ShareMenuViewDelegate,UIWebViewDelegate,MoreMenuViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property(strong,nonatomic)UIActivityIndicatorView *activityIndicatorView;
@property(strong,nonatomic)ShareMenuView *shareMenuView;

@property(strong,nonatomic)MoreMenuView *moreMenuView;

@property(strong,nonatomic)UIView *tapView;
@end

@implementation InfoDetailViewController
-(void)dealloc{
    [self.moreMenuView removeFromSuperview];
    self.moreMenuView=nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets=NO;
//    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame=CGRectMake(0, 0, 25, 25);
//    [button setImage:[UIImage imageNamed:@"share_icon.png"] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(chickShareButton:) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *rightBarButton=[[UIBarButtonItem alloc]initWithCustomView:button];
//    self.navigationItem.rightBarButtonItem=rightBarButton;
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]]];
    self.webView.delegate=self;
    
    self.activityIndicatorView=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [self.view addSubview:self.activityIndicatorView];
    self.activityIndicatorView.center=CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
    self.activityIndicatorView.activityIndicatorViewStyle=UIActivityIndicatorViewStyleGray;
    
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 25, 25);
    [button setImage:[UIImage imageNamed:@"new_back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(chickLeftBarButtonItem:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:button];
    
//    button=[UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame=CGRectMake(0, 0, 25, 25);
//    [button setImage:[UIImage imageNamed:@"new_close"] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(chickCloseBarButtonItem:) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *closeBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItems=@[leftBarButtonItem];
    
    button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 25, 25);
    [button setImage:[UIImage imageNamed:@"nav_more"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(chickMoreBarButtonItem:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *moreBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.rightBarButtonItems=@[moreBarButtonItem];
    
    
    self.tapView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:self.tapView];
    self.tapView.hidden=YES;
    [self.tapView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)]];
}

-(void)tap:(UITapGestureRecognizer *)sender{
    self.moreMenuView.hidden=YES;
    self.tapView.hidden=YES;
}

-(void)chickMoreBarButtonItem:(UIButton *)sender{
    if(self.moreMenuView==nil){
        self.moreMenuView=[[[NSBundle mainBundle]loadNibNamed:@"MoreMenuView" owner:self options:nil] lastObject];
        self.moreMenuView.width=92;
        self.moreMenuView.height=103;
        self.moreMenuView.x=SCREEN_WIDTH-92;
        self.moreMenuView.y=64-5;
        [self.navigationController.view addSubview:self.moreMenuView];
        self.moreMenuView.hidden=YES;
        self.moreMenuView.delegate=self;
    }
    if(self.moreMenuView.hidden==YES){
        self.moreMenuView.hidden=NO;
        [self.navigationController.view bringSubviewToFront:self.moreMenuView];
        self.tapView.hidden=NO;
    }else{
        self.moreMenuView.hidden=YES;
        self.tapView.hidden=YES;
    }
}

-(void)chickLeftBarButtonItem:(UIBarButtonItem *)sender{
    if(self.webView.canGoBack){
        [self.webView goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)chickCloseBarButtonItem:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)chickRefreshBarButtonItem:(UIBarButtonItem *)sender{
    [self.webView reload];
}


-(void)chickShareButton:(UIButton *)sender{
    //分享
    
    
    if(self.shareMenuView==nil){
        ShareMenuView *shareMenuView=[[ShareMenuView alloc] initWithFrame:self.view.bounds];
        self.shareMenuView=shareMenuView;
        shareMenuView.delegate=self;
        [self.navigationController.tabBarController.view addSubview:self.shareMenuView];
    }
    [self.shareMenuView show];
}

- (void)shareMenuView:(ShareMenuView *)shareMenuView collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(self.urlStr.length!=0){
//        NSRange range=[self.urlStr rangeOfString:@":/"];
//        NSMutableString *tempStr=[[NSMutableString alloc]initWithString:self.urlStr];
//        [tempStr deleteCharactersInRange:NSMakeRange(0, range.length+4)];
        NSMutableString *tempStr=[[NSMutableString alloc]initWithString:self.urlStr];
        switch (indexPath.row) {
            case -1:{
                //新浪微博
                if([WeiboSDK isWeiboAppInstalled]){
                    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
                    authRequest.redirectURI = sinaAppRedirectUri;
                    authRequest.scope = @"all";
                    
                    
                    
                    WBMessageObject *message = [WBMessageObject message];
                    message.text=self.noticeTitle;
                    WBWebpageObject *webpage = [WBWebpageObject object];
                    webpage.objectID = @"identifier1";
                    webpage.title = NSLocalizedString(self.noticeTitle, nil);
                    webpage.description = [NSString stringWithFormat:NSLocalizedString(self.noticeTitle, nil), [[NSDate date] timeIntervalSince1970]];
                    webpage.thumbnailData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_2" ofType:@"jpg"]];
                    webpage.webpageUrl = tempStr;
                    message.mediaObject = webpage;
                    
                    
                    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message authInfo:authRequest access_token:nil];
                    request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO;
                    
                    [WeiboSDK sendRequest:request];
                }else{
                    [[UMSocialControllerService defaultControllerService] setShareText:tempStr shareImage:nil socialUIDelegate:nil];
                    //        //设置分享内容和回调对象
                    [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina].snsClickHandler(self,[UMSocialControllerService defaultControllerService],YES);
                }
            }
                break;
            case 0:{
                //qq好友
                [UMSocialData defaultData].extConfig.qqData.url=tempStr;
                [UMSocialData defaultData].extConfig.qqData.title = self.noticeTitle;
                UMSocialUrlResource *resource=[UMSocialUrlResource alloc];
                [resource setResourceType:UMSocialUrlResourceTypeImage url:shareDefaultImageUrl];
                [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQQ] content:self.noticeTitle image:nil location:nil urlResource:resource presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        NSLog(@"分享成功！");
                    }
                }];
            }
                break;
            case 1:{
                //qq空间
                [UMSocialData defaultData].extConfig.qzoneData.url=tempStr;
                [UMSocialData defaultData].extConfig.qzoneData.title = self.noticeTitle;
                UMSocialUrlResource *resource=[UMSocialUrlResource alloc];
                [resource setResourceType:UMSocialUrlResourceTypeImage url:shareDefaultImageUrl];
                [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQzone] content:self.noticeTitle image:nil location:nil urlResource:resource presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        NSLog(@"分享成功！");
                    }
                }];
            }
                break;
            case 2:{
                //微信好友
                [UMSocialData defaultData].extConfig.wechatSessionData.url = tempStr;
                [UMSocialData defaultData].extConfig.wechatSessionData.title = self.noticeTitle;
                UMSocialUrlResource *resource=[UMSocialUrlResource alloc];
                [resource setResourceType:UMSocialUrlResourceTypeImage url:shareDefaultImageUrl];
                [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:self.noticeTitle image:nil location:nil urlResource:resource presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        NSLog(@"分享成功！");
                    }
                }];
            }
                break;
            case 3:{
                //朋友圈
                [UMSocialData defaultData].extConfig.wechatTimelineData.url = tempStr;
                [UMSocialData defaultData].extConfig.wechatTimelineData.title = self.noticeTitle;
                UMSocialUrlResource *resource=[UMSocialUrlResource alloc];
                [resource setResourceType:UMSocialUrlResourceTypeImage url:shareDefaultImageUrl];
                [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:self.noticeTitle image:nil location:nil urlResource:resource presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        NSLog(@"分享成功！");
                    }
                }];
            }
                break;
            case 4:{
                //短信
                [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToSms] content:[NSString stringWithFormat:@"%@ %@",self.noticeTitle,tempStr] image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        NSLog(@"分享成功！");
                    }
                }];
            }
                break;
            case 5:{
                //邮箱
                [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToEmail] content:[NSString stringWithFormat:@"%@ %@",self.noticeTitle,tempStr] image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        NSLog(@"分享成功！");
                    }
                }];
                
            }
                break;
            default:
                break;
        }
    
    }
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.moreMenuView.hidden=YES;
}

-(void)viewDidAppear:(BOOL)animated{
    [self.view bringSubviewToFront:self.activityIndicatorView];
}

#pragma mark - MoreMenuView delegate
- (void)moreMenuView:(MoreMenuView *)moreMenuView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    moreMenuView.hidden=YES;
    self.tapView.hidden=YES;
    if(indexPath.row==0){
        [self.webView reload];
    }else if (indexPath.row==1){
        if(self.shareMenuView==nil){
            ShareMenuView *shareMenuView=[[ShareMenuView alloc] initWithFrame:self.view.bounds];
            self.shareMenuView=shareMenuView;
            shareMenuView.delegate=self;
            [self.navigationController.tabBarController.view addSubview:self.shareMenuView];
        }
        [self.shareMenuView show];
    }
}

#pragma mark - webView delegate
- (void)webViewDidStartLoad:(UIWebView *)webView{
    //    [SVProgressHUD show];
    [self.activityIndicatorView startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    //    [SVProgressHUD dismiss];
    [self.activityIndicatorView stopAnimating];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    //    [SVProgressHUD showErrorWithStatus:@"网络错误"];
    [self.activityIndicatorView stopAnimating];
}

@end
